﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Diagnostics;
using System.Reflection;
using System.IO;

namespace Meraz.Core.Config
{
    public class ConfigHelper
    {
        public static string AdminEmail
        {
            get { return WebConfigurationManager.AppSettings["AdminEmail"].ToString(); }
        }


        public static string CacheObjects
        {
            get { return WebConfigurationManager.AppSettings["CacheObjects"].ToString(); }
        }

        public static string From
        {
            get
            {
                System.Net.Configuration.SmtpSection smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection;
                return smtpSection.Network.UserName;
            }
        }
        public static string TwitterGenreDataFile { get { return GetFile("TwitterGenreDataFile"); } }
        public static string TwitterUploadDirectory { get { return GetFile("TwitterUploadDirectory"); } }

        public static string BlogsFile { get { return GetFile("BlogsFile"); } }

        public static string LinkRegexPattern { get { return GetXmlValue("LinkRegexPattern"); } }

        static string GetFile(string key)
        {
            string value = null;
            try
            {
                value = WebConfigurationManager.AppSettings[key].ToString();
                Debug.WriteLine("File found for key [{0}] = '{1}'", key, value);
            }
            catch (Exception)
            {
                throw new ApplicationException(string.Format("Missing Setting while seeking '{0}'", key));
            }
            if (HttpContext.Current != null)
                return HttpContext.Current.Server.MapPath(value);

            if (Path.IsPathRooted(value))
                return value;
            else
            {
                throw new NotImplementedException();
            }
        }
        static string GetXmlValue(string key)
        {
            string value = WebConfigurationManager.AppSettings[key].ToString();
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            HttpUtility.HtmlDecode(value, writer);
            return sb.ToString();
        }

        public static System.Data.IDbConnection GetConnection()
        {
            System.Data.IDbConnection conn = null;

            string connString = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
            conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
            return conn;
        }

        public static Configuration GetExeConfig()
        {
            return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }


        public static ConfigurationSection GetSection(string sectionName)
        {
            return (ConfigurationSection)WebConfigurationManager.GetSection(sectionName);
        }

        public static ConfigurationSection GetSection(Config.ConfigSection section)
        {
            return (ConfigurationSection)WebConfigurationManager.GetSection(section.ToString());
        }

        public static ConfigurationSectionGroup GetSectionsBySectionGroup(string sectionGroup)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
            return config.GetSectionGroup(sectionGroup);
        }
        public static void SaveConfigurationSections(string sectionName, string sectionGroup, IEnumerable<ConfigurationSection> sections)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
            ConfigurationSectionGroup group = config.GetSectionGroup(sectionGroup);
            group.Sections.Clear();
            foreach (ConfigurationSection section in sections)
            {
                group.Sections.Add(sectionName, section);
            }
            config.Save();
        }
    }
}
